import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../components/Home.vue'
import About from '../components/About.vue'
import TodoList from '../components/TodoList.vue'
import Login from '../components/Login.vue'
import { useUserStore } from '../store/user'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About },
    { path: '/login', component: Login },
    { 
      path: '/todolist', 
      component: TodoList, 
      meta: { requiresAuth: true } // 需要登录保护
    }
  ]
})

// 全局路由守卫：拦截需要登录的页面
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  if (to.meta.requiresAuth && !userStore.isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router